package com.esender.strategy.mq;

import com.esender.common.constant.RabbitMQConstants;
import com.esender.common.exception.StrategyException;
import com.esender.common.model.StandardSubmit;
import com.esender.strategy.filter.StrategyFilterContext;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class PreSendListener {
    @Autowired
    private StrategyFilterContext strategyFilterContext;
    @RabbitListener(queues = {RabbitMQConstants.SMS_PRE_SEND})
    public void listen(StandardSubmit submit, Message message, Channel channel) throws IOException {
        log.info("【策略模块-接收消息】 接收到接口模块发送的消息 submit = {}",submit);
        // 处理业务
        try {
            strategyFilterContext.startStrategyFilter(submit);
            log.info("【策略模块-处理消息】 处理消息完成");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (StrategyException e) {
            log.error("【策略模块-接收消息】消费消息失败,原因是短信校验链校验失败:errorMsg:{}",e.getMessage());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}
